package com.example.gulimall.product.app.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.example.gulimall.product.entity.AttrEntity;
import com.example.gulimall.product.service.AttrAttrgroupRelationService;
import com.example.gulimall.product.service.AttrService;
import com.example.gulimall.product.service.CategoryService;
import com.example.gulimall.product.vo.AttrGroupRelationVo;
import com.example.gulimall.product.vo.CategoryWithAttrResVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.gulimall.product.entity.AttrGroupEntity;
import com.example.gulimall.product.service.AttrGroupService;
import com.example.common.utils.PageUtils;
import com.example.common.utils.R;



/**
 * 属性分组
 *
 * @author lizhichao
 * @email sunlightcs@gmail.com
 * @date 2021-08-15 16:09:01
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    CategoryService categoryService;
    @Autowired
    AttrService attrService;
    @Autowired
    AttrAttrgroupRelationService relationService;
    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
   // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catId") Long catId){
      //  PageUtils page = attrGroupService.queryPage(params);
        PageUtils page=attrGroupService.queryPage(params,catId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
  //  @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        //根据属性id获取属性对象
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catId=attrGroup.getCatelogId();
        //根据属性对象中的分类id去查找分类信息
        Long[] catagoryPath= categoryService.getFindCategoryPath(catId);
        //将传递进来的属性id获取到的属性对象的分类id树，一起返回用于回显级联菜单
        attrGroup.setCatelogPath(catagoryPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroupEntity){
		attrGroupService.save(attrGroupEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
  //  @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
  //  @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrGroup(@PathVariable("attrgroupId") Long attgroupId){
      List<AttrEntity> attrEntities=  attrService.getRelationAttr(attgroupId);
      return R.ok().put("data",attrEntities);
    }
    @PostMapping("/attr/relation/delete")
    public  R deleteRelation(@RequestBody List<AttrGroupRelationVo> vos){
         attrGroupService.deleteRelation(vos);
        return  R.ok();
    }
    @GetMapping("/{attrgroupId}/noattr/relation")
    public  R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,@RequestParam Map<String, Object> params){
       PageUtils page=attrService.getNoRelation(attrgroupId,params);
        return R.ok().put("data",page);
    }
    @PostMapping("/attr/relation")
    public  R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        relationService.addRelation(vos);
        return R.ok();
    }

    /**
     * /product/attrgroup/{catelogId}/withattr
     * @param
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public  R getWithattr(@PathVariable("catelogId") Long catelogId){
        List<CategoryWithAttrResVo> data=attrGroupService.getWithattr(catelogId);
        return R.ok().put("data",data);
    }

}
